import { _t } from '../../../i18n/base'
import { savePower, getPower } from '../../../api/user/user'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    userId: '',
    powerObj: {
      notice: 1,
      secret: 1,
      powerId: '',
      userId: '',
    },
  },
  // 获取是否开启位置权限
  getLocaStatus(e) {
    let that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success: () => {},
            fail() {
              wx.showModal({
                title: '需要获取您的地理位置授权,现在去授权吗?',
                content: '',
                showCancel: true,
                cancelText: this.data.lang['取消'],
                cancelColor: '#000000',
                confirmText: this.data.lang['确定'],
                confirmColor: '#3CC51F',
                success: (result) => {
                  if (result.confirm) {
                    wx.openSetting({
                      withSubscriptions: true,
                      success: (res) => {
                        that.savePower(e)
                        if (res.authSetting['scope.userLocation'] == true) {
                          this.savePower(this.data.powerObj)
                          that.getPower(this.data.userId)
                          wx.reLaunch({
                            url: '../../../pages/my/setting/setting',
                          })
                        }
                      },
                    })
                  }
                },
              })
            },
          })
        }
      },
    })
  },
  // 获取用户权限
  async getPower(e) {
    let { result } = await getPower({ userId: e })
    if (result == null) {
      this.savePower(this.data.powerObj)
    } else {
      this.setData({
        ['powerObj.notice']: result.notice,
        ['powerObj.secret']: result.secret,
        ['powerObj.powerId']: result.powerId,
        ['powerObj.userId']: result.userId,
      })
    }
  },
  // 更改用户权限
  async savePower(e) {
    let { result } = await savePower(e)
    if (result.userId) {
      this.getPower(this.data.powerObj.userId)
    }
  },
  // 通知
  onChanged({ detail }) {
    this.setData({
      ['powerObj.notice']: detail ? 1 : 0,
    })
    if (this.data.powerObj.notice != 1) {
      Dialog.confirm({
        message: this.data.lang['请留意：关闭通知后，您将无法收到停留日数的通知'],
        confirmButtonText: this.data.lang['确定'],
        cancelButtonText: this.data.lang['取消'],
      })
        .then(() => {
          this.getLocaStatus()
        })
        .catch(() => {
          this.setData({
            ['powerObj.notice']: 1,
          })
        })
    } else {
      this.savePower(this.data.powerObj)
    }
  },
  // 权限
  onChange({ detail }) {
    this.setData({
      ['powerObj.secret']: detail ? 1 : 0,
    })
    if (this.data.powerObj.secret != 1) {
      Dialog.confirm({
        message: this.data.lang['请留意：关闭位置权限后，您将无法使用天气、停留日数及公共服务的查询'],
        confirmButtonText: this.data.lang['确定'],
        cancelButtonText: this.data.lang['取消'],
      })
        .then(() => {
          this.savePower(this.data.powerObj)
        })
        .catch(() => {
          this.setData({
            ['powerObj.secret']: 1,
          })
        })
    } else {
      this.getLocaStatus(this.data.powerObj)
      this.savePower(this.data.powerObj)
    }
  },
  // 初始化
  onLoad() {
    let lang = _t()
    let userId = wx.getStorageSync('userId')
    this.setData({
      userId,
      lang,
      ['powerObj.userId']: userId,
    })
    wx.setNavigationBarTitle({
      title: lang['设置'],
    })
    this.getPower(userId)
  },
})
